package com.bawei.module_list

import android.graphics.Typeface

import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.widget.TextView
import com.bawei.module_base.net.bean.list.HotTop
import com.bawei.module_base.net.bean.list.NewcomerTop
import com.bawei.module_base.ui.base.BaseFragment
import com.bawei.module_list.adapter.HotTopAdapter
import com.bawei.module_list.adapter.NewcomerTopAdapter
import com.bawei.module_list.databinding.FragmentListBinding
import com.bawei.module_list.viewmodel.ListViewModel
import com.google.android.material.tabs.TabLayout
import com.stx.xhb.xbanner.transformers.ZoomPageTransformer


class ListFragment : BaseFragment<ListViewModel, FragmentListBinding>() {
    val titleArr= listOf<String>("最热主播榜","最佳新人榜")
    var labsHotTop= mutableListOf<HotTop>()
    var labsNewcomerTop= mutableListOf<NewcomerTop>()
    override fun initData() {
        //设置tab标题
        titleArr.forEach{titleA->
            dataBinding.tbList.addTab(dataBinding.tbList.newTab().setText(titleA))
        }
        //默认为新人 网络请求为1 （最佳新人）
        dataBinding.tbList.getTabAt(1)?.select()
        viewModel.tabPosition(1)

        //预加载
        dataBinding.vpList.offscreenPageLimit=2
        //设置动画
//        dataBinding.vpList.setPageTransformer(true, ArcDownTransformer())
        dataBinding.vpList.setPageTransformer(true, ZoomPageTransformer())

        //网络请求
        viewModel.getFindLeaderBoard()
        viewModel.dataFindLeaderBoard.observe(this){
            //每次执行前先清空
            labsHotTop.clear()
            labsNewcomerTop.clear()
            it?.result?.let {
                //添加
                it1->labsHotTop.addAll(it1.hotTopList)
                labsNewcomerTop.addAll(it1.newcomerTopList)
            }
            //下标为0是最热  为其他是新人
            viewModel.tabPositionIndex.observe(this){
                if (it==0){
                    dataBinding.vpList.adapter= context?.let { HotTopAdapter(it,labsHotTop) }
                }else if(it==1){
                    dataBinding.vpList.adapter= context?.let { NewcomerTopAdapter(it,labsNewcomerTop) }
                }
            }
            dataBinding.vpList.adapter?.notifyDataSetChanged()
        }


        dataBinding.tbList.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                Log.i("TAG___",""+tab?.position)
                tab?.position?.let { viewModel.tabPosition(it) }
                setTab(tab)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
    }
    //选中切换大小
    fun setTab(tab: TabLayout.Tab?){
        val textView= TextView(activity)
        //字体样式
        val selectedSize =
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20f, resources.displayMetrics)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize)
        textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD) //加粗
        textView.gravity = Gravity.CENTER
        //选中的字体颜色
        //textView.setTextColor(ContextCompat.getColor(context, Color.BLACK))
        textView.text = tab!!.text
        tab.customView = textView

    }
    override fun getViewModelClass(): Class<ListViewModel> {
        return ListViewModel::class.java
    }

    override fun getLayout(): Int {
       return R.layout.fragment_list
    }

}